package com.leetcode.August;

/**
 * @Description: 69. x 的平方根
 * 再次进入循环 while (low <= high)，此时 low = 3，high = 2，条件不成立（low 已经大于 high
 * @Author: Mr.Fxy
 * @CreateTime: 2024-09-05 09:42
 */
public class LC69x的平方根 {
    public int mySqrt(int x) {
        if (x < 2) {
            return x;
        }
        int low = 1, high = x / 2;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            long square = (long) mid * mid;
            if (square == x) {
                return mid;
            } else if (square < x) {
                low = mid + 1;
            } else {
             high=mid-1;
            }
        }
        return high;
    }
}
